package NISO;

import java.awt.Dimension; 
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Scanner;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.Timer;

public class LoginPanel extends JPanel implements ActionListener{
	private JTextField HostBox,PortBox,UserBox;
	private JPasswordField Pass;
	private JButton Done, Cancel;
	public String host, user, password; //<---all data input stored here
	public int port;//<---all data input stored here
	public String prevhost, prevuser, prevpassword; //<---all data input stored here
	public int prevport;//<---all data input stored here
		JFrame frame;
	
	public LoginPanel(final JFrame frame, String user, String pass, String host, int port){
		this.frame=frame;
		setFocusable(true);
		addKeyListener(new ML());
		this.port	 = port;
		this.host	 = host;
		this.user     = user;
		this.password = pass;
	
		setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
		
		JPanel myPanel = new JPanel();
		myPanel.setLayout(new BoxLayout(myPanel, BoxLayout.LINE_AXIS));
		
		JPanel portPanel = new JPanel();
		portPanel.setLayout(new BoxLayout(portPanel, BoxLayout.LINE_AXIS));
		JPanel hostPanel = new JPanel();
		hostPanel.setLayout(new BoxLayout(hostPanel, BoxLayout.LINE_AXIS));
		JPanel userPanel = new JPanel();
		userPanel.setLayout(new BoxLayout(userPanel, BoxLayout.LINE_AXIS));
		JPanel passPanel = new JPanel();
		passPanel.setLayout(new BoxLayout(passPanel, BoxLayout.LINE_AXIS));
		JPanel okPanel = new JPanel();
		okPanel.setLayout(new BoxLayout(okPanel, BoxLayout.LINE_AXIS));
	
		myPanel.add(new JLabel("MySQL"));
		add(myPanel);
		myPanel.setBorder(BorderFactory.createEmptyBorder(6, 7, 5, 5));//creates a space before the ok :)
		
		portPanel.add(new JLabel("    Port Number :       "));
		portPanel.add(PortBox= new JTextField(""+this.getPort()));
		add(portPanel); 
		
		hostPanel.add(new JLabel("    Host :                      "));
		hostPanel.add(HostBox= new JTextField(this.getHost()));
		add(hostPanel);
		
		
		userPanel.add(new JLabel("    Username :           "));
		userPanel.add(UserBox= new JTextField(this.getUser()));
		add(userPanel);
		
		passPanel.add(new JLabel("    Password :           "));
		passPanel.add(Pass=new JPasswordField(this.getPass()));
		add(passPanel);
		
		
		okPanel.setBorder(BorderFactory.createEmptyBorder(7, 8, 5, 5));//creates a space before the ok :)
		okPanel.add(Done= new JButton("Ok"));
		okPanel.add(Box.createRigidArea(new Dimension(50, 0)));
		okPanel.add(Cancel= new JButton("Cancel"));
		add(okPanel);
	 	
		PortBox.addActionListener( new OK());
		PortBox.addKeyListener(new Enter());
	 	HostBox.addActionListener(new OK());
	 	HostBox.addKeyListener(new Enter());
	 	UserBox.addActionListener(new OK());
		UserBox.addKeyListener(new Enter());
		Pass.addActionListener(new OK());
		Pass.addKeyListener(new Enter());
		
		Done.addActionListener(new OK());
		
		Cancel.addActionListener(
				
				 new ActionListener(){
						
						public void actionPerformed(ActionEvent event){
							if(Cancel.isEnabled())
								frame.dispose();
								
						}
					}		
		
		);
		
		Timer time = new Timer(5, this);
		time.start();
				
	}
	
	public int getPort(){
		return port;
	}
	public String getHost(){
		return host;
	}
	
	public String getUser(){
		return user;
	}
	
	public String getPass(){
		return password;
	}	
	
	@Override
	public void actionPerformed(ActionEvent arg0) {
		// TODO Auto-generated method stub
	 
		repaint();
	}
	public class ML implements MouseListener, KeyListener{

		@Override
		public void keyPressed(KeyEvent e) {
			// TODO Auto-generated method stub
			if(e.getKeyCode() == e.VK_ESCAPE)
			 System.exit(0);
			
			 
		}

		@Override
		public void keyReleased(KeyEvent e) {
			// TODO Auto-generated method stub
			 
		}

		@Override
		public void keyTyped(KeyEvent e) {
			// TODO Auto-generated method stub
			if(e.getKeyCode() == e.VK_ESCAPE)
				 System.exit(0);
		}

		@Override
		public void mouseClicked(MouseEvent e) {
			// TODO Auto-generated method stub
		 
		}

		@Override
		public void mouseEntered(MouseEvent e) {
			// TODO Auto-generated method stub
			 
			
		}

		@Override
		public void mouseExited(MouseEvent e) {
			// TODO Auto-generated method stub
			 
		}

		@Override
		public void mousePressed(MouseEvent e) {
			// TODO Auto-generated method stub
		 
			
		}

		@Override
		public void mouseReleased(MouseEvent e) {
			// TODO Auto-generated method stub
			
		}
	}
	public class OK implements ActionListener{

		
		@Override
		public void actionPerformed(ActionEvent arg0) {
			// TODO Auto-generated method stub
			//PortBox enter and confirm :)
			  String p= PortBox.getText();
			 try{
				 port=Integer.parseInt(p);//<----doesn't accept more than 6 numbers :) don't know why?
				
				}catch(Exception ex){
					 System.out.print("Invalid input");
				 }

			 host=HostBox.getText(); 
			 user=UserBox.getText();
			 password=Pass.getText();  
			frame.dispose();
			
		}
		
		
	}
	public class Enter implements KeyListener{

		@Override
		public void keyPressed(KeyEvent Key) {

			
			if (Key.getSource()==PortBox&&Key.getKeyCode()==KeyEvent.VK_ENTER)
				HostBox.requestFocus();
			else if(Key.getSource()==HostBox&&Key.getKeyCode()==KeyEvent.VK_ENTER)
				UserBox.requestFocus();
			else if(Key.getSource()==UserBox&&Key.getKeyCode()==KeyEvent.VK_ENTER)
				Pass.requestFocus();
			else if(Key.getSource()==Pass&&Key.getKeyCode()==KeyEvent.VK_ENTER){
				frame.dispose();
			}
				
			
		}


		@Override
		public void keyReleased(KeyEvent arg0) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void keyTyped(KeyEvent arg0) {
			// TODO Auto-generated method stub
			
		}
		
	}
	
	
}
